package com.wxzz.elearing.pay.api.app;


import com.alibaba.fastjson.JSONObject;
import com.wxzz.elearing.pay.dto.UserBankCardDto;
import com.wxzz.elearing.pay.dto.UserTransactionDto;
import com.wxzz.elearing.pay.entity.UserBankCard;
import com.wxzz.elearing.pay.entity.UserTransaction;
import com.wxzz.elearing.pay.service.IUserBankCardService;
import com.wxzz.elearning.common.vo.JgPageVo;
import com.wxzz.elearning.common.vo.ReturnVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author
 * @since 2021-04-23
 */
@RestController
@RequestMapping("/wx-user-bank-card")
@Api(tags = "银行卡接口(小程序)")
public class WxUserBankCardController {

    @Autowired
    private IUserBankCardService userBankCardService;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @ApiOperation(value = "添加或修改银行卡(小程序)", notes = "添加或修改银行卡(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "saveUserBankCard", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<UserTransaction>>> saveUserBankCard(@RequestHeader String token, @RequestBody UserBankCard userBankCard) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        userBankCard.setUserType(2);
        return userBankCardService.saveUserBankCard(jsonObject,userBankCard);
    }


    @ApiOperation(value = "列出我的银行卡(小程序)", notes = "列出我的银行卡(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "listUserBankCardByUser", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<UserBankCard>>> listUserBankCardByUser(@RequestHeader String token, @RequestBody UserBankCardDto userBankCardDto) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        userBankCardDto.setUserType(2);
        return userBankCardService.listUserBankCardByUser(jsonObject,userBankCardDto);
    }

    @ApiOperation(value = "查询我的默认银行卡(小程序)", notes = "查询我的默认银行卡(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "selectDefault", method = RequestMethod.POST)
    public ReturnVO<UserBankCard> selectDefault(@RequestHeader String token) {
        JSONObject jsonObject = JSONObject.parseObject(redisTemplate.opsForValue().get(token));
        return userBankCardService.selectDefault(jsonObject,2);
    }

    @ApiOperation(value = "删除银行卡(小程序)", notes = "删除银行卡(小程序)", httpMethod = "POST", tags = "小程序")
    @RequestMapping(value = "deleteUserBankCardById/{userBankCardId}", method = RequestMethod.POST)
    public ReturnVO<JgPageVo<List<UserBankCard>>> deleteUserBankCardById(@PathVariable Long userBankCardId) {
        return userBankCardService.deleteUserBankCardById(userBankCardId);
    }
}
